"""
v0.19.1 (November 3, 2016) new features
"""
from io import StringIO

import numpy as np
import pandas as pd


def merge_asof_test():
    left = pd.DataFrame({'a': [1, 5, 10],
                        'left_val': ['a', 'b', 'c']})

    right = pd.DataFrame({'a': [1, 2, 3, 6, 7],
                        'right_val': [1, 2, 3, 6, 7]})
    print(left)
    print(right)
    print(pd.merge_asof(left, right, on='a'))
    print(pd.merge_asof(left, right, on='a', allow_exact_matches=False))


def rolling_test():
    dft = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]},
                    index=pd.Index([pd.Timestamp('20130101 09:00:00'),
                                    pd.Timestamp('20130101 09:00:02'),
                                    pd.Timestamp('20130101 09:00:03'),
                                    pd.Timestamp('20130101 09:00:05'),
                                    pd.Timestamp('20130101 09:00:06')],
                                   name='foo'))
    print(dft)
    dft = dft.reset_index()
    print(dft)
    print(dft.rolling('2s', on='foo').sum())


def read_csv_test():
    data = '0,1,2\n3,4,5'
    names = ['a', 'b', 'a']
    df = pd.read_csv(StringIO(data), names=names)
    print(df)

def read_csv_test2():
    data = 'col1,col2,col3\na,b,1\na,b,2\nc,d,3'
    # df = pd.read_csv(StringIO(data))
    # df = pd.read_csv(StringIO(data), dtype='category')
    df = pd.read_csv(StringIO(data), dtype={'col1': 'category'})
    print(df)
    print(df.dtypes)

def read_csv_chunk_test():
    data = 'A,B\n0,1\n2,3\n4,5\n6,7'
    df = pd.concat(pd.read_csv(StringIO(data), chunksize=2))
    print(df)

def to_list_test():
    s = pd.Series([1, 2, 3])
    print(type(s.tolist()))

def merge_test():
    df1 = pd.DataFrame({'key': [1], 'v1': [10]})
    df2 = pd.DataFrame({'key': [1, 2], 'v1': [20, 30]})
    df = pd.merge(df1, df2, how='outer')
    print(df)
    print(df.dtypes)
    print(pd.merge(df1, df2, how='outer', on='key'))

def date_period_test():
    pi = pd.PeriodIndex(['2016-08-01'], freq='D')
    print(pi)

if __name__ == '__main__':
    # merge_asof_test()
    # rolling_test()
    # read_csv_test()
    # read_csv_test2()
    read_csv_chunk_test()
    # to_list_test()
    # merge_test()
    # date_period_test()